﻿using Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RegisterPanel : BasePanel
{
    private InputField usernameIF;
    private InputField passwordIF;
    private InputField rePasswordIF;
    private RegisterRequest registerRequest;

    private void Start()
    {
        usernameIF = transform.Find("Username/UsernameInput").GetComponent<InputField>();
        passwordIF = transform.Find("Password/PasswordInput").GetComponent<InputField>();
        rePasswordIF = transform.Find("RePassword/RePasswordInput").GetComponent<InputField>();

        transform.Find("RegisterBtn").GetComponent<Button>().onClick.AddListener(OnRegisterClick);
        transform.Find("CloseBtn").GetComponent<Button>().onClick.AddListener(OnCloseClick);
        registerRequest = GetComponent<RegisterRequest>();
    }
    public override void OnEnter()
    {
        gameObject.SetActive(true);
    }
    private void OnRegisterClick()
    {
        if (string.IsNullOrEmpty(usernameIF.text))
        {
            uiMng.ShowMessage("用户名不能为空");
            return;
        }
        if (string.IsNullOrEmpty(passwordIF.text))
        {
            uiMng.ShowMessage("密码不能为空");
            return;
        }
        if (rePasswordIF.text!=passwordIF.text)
        {
            uiMng.ShowMessage("两次密码输入不一致！");
            return;
        }

        registerRequest.SendRequest(usernameIF.text, passwordIF.text);
    }
    public void OnRegisterResponse(ReturnCode returnCode)
    {
        string msg = "";
        if (returnCode == ReturnCode.Success)
        {
            msg = "注册成功";
        }
        else
        {
            msg = "用户名重复";
        }
        uiMng.ShowAsyncMessage(msg);
    }
    private void OnCloseClick()
    {
        uiMng.HidePanel(UIPanelType.Register);
        uiMng.OpenPanel(UIPanelType.Login);
    }
    public override void OnHide()
    {
        gameObject.SetActive(false);
    }
}
